# -*- coding:utf-8 -*-

import urllib
import urllib2

# request = urllib2.Request('http://www.baidu.com')
# response = urllib2.urlopen(request)
# print response.read()

"""
    zwx 2017-12-08
    基本的post、get请求方法
"""

def base_post_method():
    #定义请求参数
    values = {}
    values['userName'] = 'egova'
    values['password'] = ''
    values['ip'] = 'egova'
    values['browserVersion'] = 'chrome/61.0.3163.100'
    values['osVersion'] = 'Win10/64'
    data = urllib.urlencode(values)
    #定义请求接口
    url = 'http://192.168.32.200:8080/eUrbanMIS/login/validpassword'
    #进行请求发送
    try:
        request = urllib2.Request(url,data)
        response = urllib2.urlopen(request)
        #请求结果打印
        print response.read()
    except urllib2.HTTPError,e:
        print e.code
        print e.reason
    except urllib2.URLError, e:
        print e.reason
    else:
        print "OK"

#基础的get请求
def base_get_method():
    #请求参数
    values = {}
    values['taskListID'] = '28'
    values['currentPage'] = '1'
    values['numPerPage'] = '10'
    data = urllib.urlencode(values)
    #请求接口
    url = 'http://192.168.32.200:8080/eUrbanMIS/home/bizbase/tasklist/gethumantasklistdata'
    get_url = url + '?' + data
    try:
        request = urllib2.Request(get_url)
        response = urllib2.urlopen(request)
        print response.read()
    except urllib2.URLError, e:
        if hasattr(e, "code"):
            print e.code
        if hasattr(e, "reason"):
            print e.reason
    else:
        print "OK"

#header用法
def base_header_method():
    # 请求参数
    values = {}
    values['userName'] = 'egova'
    values['password'] = ''
    values['ip'] = 'egova'
    values['browserVersion'] = 'chrome/61.0.3163.100'
    values['osVersion'] = 'Win10/64'
    data = urllib.urlencode(values)
    #请求的header
    request_header = {}
    request_header['Cache-Control'] = 'no-cache, no-store, max-age=0'
    request_header['Content-Language'] = 'zh-CN'
    # request_header['Content-Type'] = 'application/json;charset=UTF-8'
    request_header['Pragma'] = 'no-cache'
    request_header['Server'] = 'Apache-Coyote/1.1'
    # request_header['Transfer-Encoding'] = 'chunked'
    # 定义请求接口
    url = 'http://192.168.32.200:8080/eUrbanMIS/login/validpassword'
    # 进行请求发送
    try:
        request = urllib2.Request(url, data,request_header)
        response = urllib2.urlopen(request)
        # 请求结果打印
        print response.read()
    except urllib2.HTTPError, e:
        print e.code
        print e.reason
    except urllib2.URLError, e:
        print e.reason
    else:
        print "OK"


if __name__ == '__main__':
    print "utils test"
    base_header_method()